﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Socket socket;
        private List<Socket> proxSocketList = new List<Socket>();
        private void btnListen_Click(object sender, EventArgs e)
        {
            string ip = this.txtIP.Text;
            try
            {
                IPAddress ipAddress = IPAddress.Parse(ip);
                IPEndPoint endPoint = new IPEndPoint(ipAddress, int.Parse(this.txtPort.Text));
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(endPoint);
                socket.Listen(10);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"出现异常{ex.Message}");
                return;
            }


            btnListen.Enabled = false;
            lbStatus.Text = "Listening......";
            txtIP.Enabled = false;
            txtPort.Enabled = false;

            Task.Run(() =>
            {
                while (true)
                {
                    Socket proxSocket = socket.Accept();
                    proxSocketList.Add(proxSocket);
                    this.Invoke(new Action(() =>
                    {
                        txtLog.Text += "新的连接:" + proxSocket.RemoteEndPoint.ToString() + "\r\n";
                    }));
                    Task.Run(() =>
                    {
                        while (true)
                        {
                            try
                            {
                                byte[] buffer = new byte[1024 * 1024];
                                int realLenth = proxSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                                string strResult = Encoding.Default.GetString(buffer, 0, realLenth);
                                this.Invoke(new Action(() =>
                                {
                                    txtLog.Text += proxSocket.RemoteEndPoint + ":" + strResult + "\r\n";
                                }));
                                byte[] sendBuffer = Encoding.Default.GetBytes(strResult);
                                foreach (var item in proxSocketList)
                                {
                                    if (item != proxSocket)
                                    {
                                        item.Send(sendBuffer, 0, sendBuffer.Length, SocketFlags.None);
                                    }                                
                                }
                            }
                            catch (SocketException)
                            {
                                this.Invoke(new Action(() =>
                                {
                                    txtLog.Text += proxSocket.RemoteEndPoint + "离开了聊天室\r\n";
                                }));
                                proxSocketList.Remove(proxSocket);
                                proxSocket.Close();

                            }
                        }
                    });
                }
            });

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void txtPort_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void txtIP_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
